package com.qf.hospital.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.hospital.constants.HospitalConstants;
import com.qf.hospital.controller.CaoController;
import com.qf.hospital.entity.Cashier;
import com.qf.hospital.entity.CashierExample;
import com.qf.hospital.enums.HospitalExceptionEnum;
import com.qf.hospital.exception.HosptialException;
import com.qf.hospital.mapper.CashierMapper;
import com.qf.hospital.service.CashierService;
import com.qf.hospital.service.PharmacyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 何星宇
 * @version V1.0
 * @Date 2021/10/21 16:01
 */

@Service
public class CashierServiceImpl implements CashierService {
    @Autowired
    private CashierMapper mapper;
    @Autowired
    private PharmacyService pharmacyService;
    private Logger log = LoggerFactory.getLogger(CaoController.class);

    /**
     * 根据患者id查询该患者的药品出纳信息
     * @param reportId 患者id
     * @param page
     * @param limit
     * @return
     */
    @Override
    public PageInfo<Cashier> findCashierByReportIdAndPage(Integer reportId, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        CashierExample example = new CashierExample();
        example.createCriteria().andReportIdEqualTo(reportId).andStateEqualTo(HospitalConstants.CASHIER_STATE_DRUG);
        List<Cashier> cashierList = mapper.selectByExample(example);
        PageInfo<Cashier> pageInfo = new PageInfo<>(cashierList);
        return pageInfo;
    }

    /**
     * 查询患者的药品缴费状态
     * @param reportId 患者id
     * @return
     */
    @Override
    public Integer findDrugPaymentStatusByReportId(Integer reportId) {
        CashierExample example = new CashierExample();
        example.createCriteria().
                andStateEqualTo(HospitalConstants.CASHIER_STATE_DRUG).
                andMstateEqualTo(HospitalConstants.CASHIER_PAYMENT_STATUS_NO).
                andReportIdEqualTo(reportId);
        long count = mapper.countByExample(example);
        return count==0?0:1;
    }

    /**
     * 查询患者是否有未缴费的项目
     * @param reportId 患者id
     * @return
     */
    @Override
    public Integer findProjectPaymentStatusByReportId(Integer reportId) {
        CashierExample example = new CashierExample();
        example.createCriteria().
                andStateEqualTo(HospitalConstants.CASHIER_STATE_PROJECT).
                andMstateEqualTo(HospitalConstants.CASHIER_PAYMENT_STATUS_NO).
                andReportIdEqualTo(reportId);
        long count = mapper.countByExample(example);
        return count==0?0:1;
    }

    /**
     * 查询患者是否有已缴费，但是未做的项目
     * @param reportId
     * @return
     */
    @Override
    public Integer findPaymentAndNoResultByReportId(Integer reportId) {
        CashierExample example = new CashierExample();
        example.createCriteria().
                andStateEqualTo(HospitalConstants.CASHIER_STATE_PROJECT).
                andOstateEqualTo(HospitalConstants.CASHIER_PROJECT_TYPE_EXAMINE).
                andJieEqualTo("").
                andReportIdEqualTo(reportId);
        long count = mapper.countByExample(example);
        return count==0?0:1;
    }

    /**
     * 根据患者id和药品名称，查询cashier出纳表中是否有未缴费的当前药品
     * @param reportId 患者id
     * @param drugName 药品名称
     * @return
     */
    @Override
    public Integer checkPaymentStatusByReportIdAndDrugName(Integer reportId, String drugName) {
        CashierExample example = new CashierExample();
        example.createCriteria().andReportIdEqualTo(reportId).andDurgNameEqualTo(drugName).andMstateEqualTo(HospitalConstants.CASHIER_PAYMENT_STATUS_NO);
        long count = mapper.countByExample(example);
        return count==0?0:1;
    }

    /**
     * 添加患者出纳信息
     * @param cashier
     */
    @Override
    public void addCashier(Cashier cashier) {
        int count = mapper.insertSelective(cashier);
        if (count!=1){
            //记录日志
            log.info("【系统异常】 添加患者出纳信息失败");
            throw new HosptialException(HospitalExceptionEnum.SYSTEM_EXCEPTION,"添加患者出纳信息失败");
        }
    }

    /**
     * 修改患者出纳信息
     * @param reportId
     * @param pharmacyName
     * @param num
     * @param totalPrice
     */
    @Override
    public void updateCashier(Integer reportId, String pharmacyName, Integer num, Double totalPrice) {
        int count = mapper.updateCashierByReportIdAndPharmacyName(reportId,pharmacyName,num,totalPrice);
    }

    /**
     * 根据处方信息id查询当前药品处方信息
     * @param id 处方信息主键id
     * @return
     */
    @Override
    public Cashier findById(Integer id) {
        Cashier cashier = mapper.selectByPrimaryKey(id);
        return cashier;
    }

    /**
     * 删除处方信息（注意添加库存）
     * @param id 处方信息主键id
     */
    @Override
    @Transactional
    public void deleteById(Integer id) {
        //获取当前药品处方信息
        Cashier cashier = findById(id);
        //获取药品信息以及数量
        String durgName = cashier.getDurgName();
        Integer durgNum = cashier.getDurgNum();
        //修改药品库存
        pharmacyService.addInventoryByPharmacyName(durgName,durgNum);
        //删除处方信息
        int count = mapper.deleteByPrimaryKey(id);
        if (count!=1){
            //记录日志
            log.info("【系统异常】 删除处方信息失败");
            throw new HosptialException(HospitalExceptionEnum.SYSTEM_EXCEPTION,"删除处方信息失败");
        }
    }

    /**
     * 根据患者id查询该患者的项目出纳信息
     * @param reportId
     * @param page
     * @param limit
     * @return
     */
    @Override
    public PageInfo<Cashier> findByReportIdAndPage(Integer reportId, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        CashierExample example = new CashierExample();
        example.createCriteria().andReportIdEqualTo(reportId);
        List<Cashier> cashierList = mapper.selectByExample(example);
        PageInfo<Cashier> pageInfo = new PageInfo<>(cashierList);
        return pageInfo;
    }

    @Override
    public void deleteProjectById(Integer cashierId) {
        //删除项目信息
        int count = mapper.deleteByPrimaryKey(cashierId);
        if (count!=1){
            //记录日志
            log.info("【系统异常】 删除项目信息失败");
            throw new HosptialException(HospitalExceptionEnum.SYSTEM_EXCEPTION,"删除项目信息失败");
        }
    }

}
